;( function () {
	"use strict";
    var param_map = Result.getLocationParamObject();
    var global_param_map;
    
    var msg_id="0";
    var timerc=60; //全局时间变量（秒数）
	
	$( function () {
		if(!ifUser()){
			return;
		}
		$("#bind").click(function(){
			bind();
		});
		
		
	}); 
	
	/**
	 * 修改绑定手机号
	 */
	function bind(){
		//进行验证
		var $form = $( "#bind_form" );
 		$form.find("input").each( function () {
	    	$( this ).keydown( function () {
	        	if($( this ).hasClass('result-invalid')){
	           		$( this ).removeClass('result-invalid');
	           	}
	        } );
        } ); 
        
	 	if ( Result.validateForm( $form ) === true ) {
        	var params = Result.getFormData( $form, true );
        	var postParams =  {};
        	postParams.phone = params.mobile;
        	postParams.smsMessageSid = msg_id;
        	postParams.smsCode = params.msg_code;
        	postParams.userId = USER.userId;
        	
        	$.ajax(
                getAjaxOps('/user/modifyPhone', postParams)
            ).done(function (res) {
                if(res.resultCode == 0){
                	if(res.data.errorCode == 0){
                		$("body").tips("修改成功");
                		window.history.go(-1);
                	} else {
                		$("body").tips(res.data.errorInfo);
                	}
                }else {
                	$("body").tips(res.resultMsg);
                }
            });
       	} 
	}
	
	
	window.add = function(){ //倒计时函数
        if(timerc >0){ //如果不到5分钟
            --timerc; //时间变量自增1
            $("#msgButton").attr("style","color:  #666666; border: 1px solid #dcdcdc;background-color:#EEEEEE");
            $("#msgButton").attr("onclick","");
            $("#msgButton").html("重新获取("+timerc+")"); 
            setTimeout("add()", 1000); //设置1000毫秒以后执行一次本函数
        }else{
        	$("#msgButton").attr("style","color:  #f23030; border: 1px solid #f23030;");
        	$("#msgButton").html("重新获取");
        	$("#msgButton").attr("onclick","getMsgCode()");
        	timerc=60;
        }
    };
	 
	
	
	/**
	 * 获取验证码
	 */
	window.getMsgCode=function(){
   		var params={};
    	var myphone=$("#myphone").val();
    	if(myphone == null || myphone == "" || myphone.length != 11){
    		alert("请输入正确的电话号码");
    		return;
    	}
    	params.phone = myphone;
    	$.ajax(
            getAjaxOps('/user/getCode', params)
        ).done(function (res) {
            if(res.resultCode == 0){
                if(res.data.templateSMS && res.data.templateSMS.smsMessageSid){
                	add();
                	$("body").tips("验证码已经发到您的手机");
                	msg_id = res.data.templateSMS.smsMessageSid;
                }else{
                	$("body").tips(res.data.statusMsg);
                }
            }else {
            	$("body").tips(res.resultMsg);
            }
        });
    };
	
	

}( window ) );
